package com.zz.ecommerce.declareManage.common;

import com.alibaba.fastjson.JSONObject;
import com.zz.ecommerce.common.GridRequest;
import com.zz.ecommerce.common.bean.insp.db.order.TOrderGoodsListInsp;
import com.zz.ecommerce.common.bean.insp.db.order.TOrderHeadInsp;
import com.zz.ecommerce.common.bean.insp.db.payment.TPaymentHeadInsp;
import com.zz.ecommerce.common.bean.insp.db.waybill.TBillHeadInsp;
import com.zz.ecommerce.common.bean.qdc.db.*;
import com.zz.ecommerce.common.constants.TableConstants;
import com.zz.ecommerce.declareManage.common.dao.*;
import com.zz.ecommerce.declareManage.common.service.QdcOrderService;
import com.zz.ecommerce.declareManage.importB2CDeclareDirect.orderManageQdc.CbecOrderDao;
import com.zz.ecommerce.declareManage.importB2CDeclareDirect.orderManageQdc.CbecOrderItemDao;
import com.zz.ecommerce.declareManage.importB2CDeclareDirect.paybillQdc.CbecPaymentDao;
import com.zz.ecommerce.declareManage.importB2CDeclareDirect.taxbill.CbecTaxDao;
import com.zz.ecommerce.declareManage.importB2CDeclareDirect.waybill.CbecLogisticsDao;
import com.zz.ecommerce.pojo.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.List;

/**
 * Created by admin on 2016/4/6.
 * <p>
 * 公共详情页跳转
 */
@Controller
@RequestMapping("/commonDetail")
@Scope("prototype")
public class CommomController {

    @Autowired
    private CbecOrderDao cbecOrderDao;

    @Autowired
    private CbecOrderItemDao cbecOrderItemDao;

    @Autowired
    private QdcOrderService orderService;

    @Autowired
    private CbecLogisticsDao cbecLogisticsDao;

    @Autowired
    private PayBillQdcDao payBillQdcDao;

    @Autowired
    private CbecPaymentDao cbecPaymentDao;

    @Autowired
    private CbecTaxDao cbecTaxDao;

    @Autowired
    private TbMergeTaxDao tbMergeTaxDao;

    @Autowired
    private TOrderHeadInspDao tOrderHeadInspDao;

    @Autowired
    private TOrderGoodsListInspDao tOrderGoodsListInspDao;

    @Autowired
    private TBillHeadInspDao tBillHeadInspDao;

    @Autowired
    private TPaymentHeadInspDao tPaymentHeadInspDao;

    @RequestMapping(value = "orderBillDetail")
    public String orderBillDetail(CbecOrder cbecOrder, GridRequest request, ModelMap modelMap) {
        if (StringUtils.isNotEmpty(cbecOrder.getId())) {
            CbecOrder orderHead = cbecOrderDao.query(TableConstants.CBEC_ORDER, cbecOrder);

            CbecOrderItem cbecOrderItem = new CbecOrderItem();
            cbecOrderItem.setOrderId(cbecOrder.getId());
            List<CbecOrderItem> goodsList = cbecOrderItemDao.queryAll(TableConstants.CBEC_ORDER_ITEM, cbecOrderItem, request.getSortOrder());
//            List<TWaybillListQdc> waybillList = orderService.queryAllWaybillList(request, query.getId());

            modelMap.put("orderHead", orderHead);
            modelMap.put("goodsListJSON", JSONObject.toJSONString(goodsList));
//            modelMap.put("waybillListJSON", JSONObject.toJSONString(waybillList));
        }
        return "declareManage/commonQdc/orderbill_detail";
    }

    @RequestMapping(value = "wayBillDetail/{id}")
    public String wayBillDetail(@PathVariable("id") String id, ModelMap map, GridRequest request) {
        CbecLogistics cbecLogistics = new CbecLogistics();
        cbecLogistics.setId(id);
        CbecLogistics waybillHeadQdc = cbecLogisticsDao.query(TableConstants.CBEC_LOGISTICS, cbecLogistics);
        map.put("waybillHead", waybillHeadQdc);

//        if (null != waybillHeadQdc && StringUtils.isNotEmpty(waybillHeadQdc.getWaybillId())) {
//            List<TWaybillListQdc> waybillList = orderService.queryAllWaybillListByWarybillId(request, waybillHeadQdc.getWaybillId());
//            map.put("waybillListJSON", JSONObject.toJSONString(waybillList));
//            List<TGoodsListQdc> goodsList = orderService.queryAllGoodsListByWarybillId(request, waybillHeadQdc.getWaybillId());
//            map.put("goodsListJSON", JSONObject.toJSONString(goodsList));
//        }
        return "declareManage/commonQdc/waybill_detail";
    }

//    @RequestMapping(value = "redirectOrderBillDetail")
//    public String redirectOrderBillDetail(TOrderHeadQdc query) {
//        TOrderHeadQdc orderHead = orderHeadDao.query(TableConstants.T_ORDER_HEAD_QDC, query);
//        return "redirect:orderBillDetail?id=" + orderHead.getId();
//    }

    @RequestMapping("paybillDetail/{id}")
    public String paybillDetail(@PathVariable("id") String id, ModelMap modelMap) {
        CbecPayment tPaymentHeadQdc = new CbecPayment();
        tPaymentHeadQdc.setId(id);
        modelMap.put("payInfo", cbecPaymentDao.query(TableConstants.CBEC_PAYMENT, tPaymentHeadQdc));
        return "declareManage/commonQdc/pay_bill_detail";
    }

    /**
     * 税单详情页
     *
     * @return
     */
    @RequestMapping(value = "taxbillDetail/{elistNo}", method = {RequestMethod.GET})
    public String taxbillDetail(@PathVariable("elistNo") String elistNo, ModelMap map) {
        CbecTax cbecTax = new CbecTax();
        cbecTax.setElistNo(elistNo);
        map.addAttribute("taxInfo", cbecTaxDao.query(TableConstants.CBEC_TAX, cbecTax));
        return "declareManage/commonQdc/taxbill_detail";
    }


    /**
     * 青岛汇总税单详情页
     *
     * @return
     */
    @RequestMapping(value = "taxbillQdcDetail/{indx}", method = {RequestMethod.GET})
    public String taxbillQdcDetail(@PathVariable("indx") String indx, ModelMap map) {
        TbMergeTax tbMergeTax = new TbMergeTax();
        tbMergeTax.setIndx(indx);
        map.addAttribute("taxInfo", tbMergeTaxDao.query(TableConstants.TB_MERGE_TAX, tbMergeTax));
        return "declareManage/commonQdc/taxbillQdc_detail";
    }

    /**
     * 订单详情--国检
     *
     * @param query
     * @param request
     * @param modelMap
     * @return
     */
    @RequestMapping(value = "orderBillInspDetail")
    public String orderBillInspDetail(TOrderHeadInsp query, GridRequest request, ModelMap modelMap) {
        if (StringUtils.isNotEmpty(query.getId())) {
            TOrderHeadInsp orderHead = tOrderHeadInspDao.query(TableConstants.T_ORDER_HEAD_INSP, query);

            TOrderGoodsListInsp tOrderGoodsListInsp = new TOrderGoodsListInsp();
            tOrderGoodsListInsp.setRelateId(query.getId());
            request.setSidx("seqno");
            request.setSord("asc");
            List<TOrderGoodsListInsp> goodsList = tOrderGoodsListInspDao.queryAll(TableConstants.T_ORDER_GOODS_LIST_INSP, tOrderGoodsListInsp, request.getSortOrder());

            modelMap.put("orderHead", orderHead);
            modelMap.put("goodsListJSON", JSONObject.toJSONString(goodsList));
        }
        return "declareManage/commonInsp/orderbill_detail";
    }

    @RequestMapping(value = "wayBillInspDetail/{id}")
    public String wayBillInspDetail(@PathVariable("id") String id, ModelMap map) {
        TBillHeadInsp tBillHeadInsp = new TBillHeadInsp();
        tBillHeadInsp.setId(id);
        TBillHeadInsp tBillHeadInsp1 = tBillHeadInspDao.query(TableConstants.T_BILL_HEAD_INSP, tBillHeadInsp);
        map.put("waybillHead", tBillHeadInsp1);
        return "declareManage/commonInsp/waybill_detail";
    }

    @RequestMapping("paybillDetailInsp/{id}")
    public String paybillDetailInsp(@PathVariable("id") String id, ModelMap modelMap) {
        TPaymentHeadInsp tPaymentHeadInsp = new TPaymentHeadInsp();
        tPaymentHeadInsp.setId(id);
        modelMap.put("payInfo", tPaymentHeadInspDao.query(TableConstants.T_PAYMENT_HEAD_INSP, tPaymentHeadInsp));
        return "declareManage/commonInsp/pay_bill_detail";
    }

}
